﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Itasa.API;

namespace Itasa.RSS
{
    internal class RSS_Sub : IComparable, IComparable<RSS_Sub>
    {

        internal long ID{ get; private set; }
        internal String Title{ get; private set; }
        internal String Link{ get; private set; }
        internal String Guid{ get; private set; }
        internal String Description { get; private set; }
        internal String Category{ get; private set; }
        internal DateTime PubDate{ get; private set; }
        internal SubInfo SubInfo { get; set; }

        internal RSS_Sub(XmlNode sub_node)
        {
            foreach (XmlNode sd in sub_node.ChildNodes)
            {
                if (sd.InnerText != null)
                {
                    if (sd.Name == "title") Title = sd.InnerText;
                    if (sd.Name == "link") Link = sd.InnerText;
                    if (sd.Name == "guid") Guid = sd.InnerText;
                    if (sd.Name == "category") Category = sd.InnerText;
                    if (sd.Name == "pubDate") PubDate = DateTime.Parse(sd.InnerText);
                }

            }
            int i = Guid.IndexOf("&id=");
            i = i + 4;
            ID = long.Parse(Guid.Substring(i, (Guid.Length - i)));
        }

        public override bool Equals(object obj)
        {
            if (obj is RSS_Sub) if (this.ID.Equals((obj as RSS_Sub).ID)) return true;
            return false;
        }

        public int CompareTo(object obj)
        {
            if (obj is RSS_Sub) return this.PubDate.CompareTo((obj as RSS_Sub).PubDate);
            else return 0;
        }

        public int CompareTo(RSS_Sub other)
        {
            if (other == null) throw new ArgumentNullException("other");
            return this.PubDate.CompareTo(other.PubDate);
        }
    }
}
